// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: H Crush Girder Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 52 : TYPE_HCRUSHGIRDER

// Function declarations
reserve function HCrushGirder_DebugDraw
reserve function HCrushGirder_DebugSpawn

// Static Values

// Tables

function HCrushGirder_DebugDraw
	DrawSprite(0)
end function


function HCrushGirder_DebugSpawn
	CreateTempObject(TypeName[H Crush Girder], 0, object.xpos, object.ypos)
	object[tempObjectPos].value1 = object.xpos
	object[tempObjectPos].value2 = object.ypos
end function


event ObjectMain
	switch object.state
	case 0
		object.value0++
		if object.value0 == 96
			object.value0 = 6
			object.value3 = 1
			object.state = 4
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 1
				object[currentPlayer].xpos += 0x10000
				break
			case 2
				object[currentPlayer].pushing = 0
				break
			end switch
		next
		object.xpos += 0x10000
		break
	case 1
		object.value0++
		if object.value0 == 48
			object.value0 = 6
			object.value3 = 2
			object.state = 4
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 1
				object[currentPlayer].ypos += 0x10000
				break
			case 4
				if object[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(PlayerObject_Kill)
				end if
				break
			end switch
		next
		object.ypos += 0x10000
		break
	case 2
		object.value0++
		if object.value0 == 96
			object.value0 = 6
			object.value3 = 3
			object.state = 4
		end if
		object.ypos -= 0x4000
		object.xpos -= 0x10000
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 1
				object[currentPlayer].xpos -= 0x10000
				break
			case 4
				break
			end switch
		next
		break
	case 3
		object.value0++
		if object.value0 == 24
			object.value0 = 6
			object.value3 = 0
			object.state = 4
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			switch checkResult
			case 1
				object[currentPlayer].ypos -= 0x10000
				break
			case 4
				break
			end switch
		next
		object.ypos -= 0x10000
		break
	case 4
		if object.value0 > 0
			object.value0--
		else
			object.state = object.value3
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -96, -24, 96, 24, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 4
				if object[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(PlayerObject_Kill)
				end if
			end if
		next
	end switch
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-96, -24, 192, 48, 319, 1)
	foreach (TypeName[H Crush Girder], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].value2 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[H Crush Girder], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HCrushGirder_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HCrushGirder_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
